package com.pzhu.user.dao;

import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.row.Db;
import com.mybatisflex.core.util.SqlUtil;
import com.pzhu.base.common.utils.mybatisflex.LogicDeleteService;
import com.pzhu.base.common.utils.mybatisflex.QueryUtils;
import com.pzhu.user.domain.entity.Role;
import com.pzhu.user.domain.ro.RoleSearchRO;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

import static com.pzhu.user.domain.entity.table.RoleTableDef.ROLE;
import static com.pzhu.user.domain.entity.table.UserRoleTableDef.USER_ROLE;

/**
 * @author 75073
 * @description 针对表【role(角色表)】的数据库操作Mapper
 * @createDate 2024-02-19 00:37:22
 */
public interface RoleDAO extends LogicDeleteService<Role> {

    /**
     * 按ID选择角色
     *
     * @param roleId 角色id
     * @return {@link Role }
     *
     */
    default Role selectRoleById(Long roleId) {
        if (roleId == null) {
            return null;
        }
        return selectOneByCondition(QueryUtils.and(QueryUtils.del(ROLE.DELETED_ID), ROLE.ID.eq(roleId)));
    }

    default Page<Role> search(Page<Role> page, RoleSearchRO roleSearchRo) {
        QueryCondition condition = QueryUtils.and(
                QueryUtils.del(ROLE.DELETED_ID),
                ROLE.CODE.like(roleSearchRo.getRoleCode()),
                ROLE.ROLE_DESC.like(roleSearchRo.getRoleName()));
        return page(page, condition);
    }

    default boolean existCode(String roleCode){
        QueryWrapper query = new QueryWrapper();
        query.select(ROLE.ID)
                .from(ROLE)
                .where(QueryUtils.and(ROLE.CODE.eq(roleCode), QueryUtils.del(ROLE.DELETED_ID)));
        return exists(query);
    }
}
